const app = getApp();

Page({
  data: {
    dynamic: {},
    comments: [],
    showComments: false,
    cdnUrl:''
  },

  onLoad(options) {
    const id = options.id;
    this.loadDynamicDetail(id);
    //this.loadComments(id);
    this.setData({
        cdnUrl:app.globalData.cdnUrl
    });
  },

  // 加载动态详情
  loadDynamicDetail(id) {
    wx.request({
        url: app.globalData.baseUrl + 'public/trend/' + id,
        data: { 
        },
        header: {
          'Authorization': `Bearer ${app.globalData.token}`
        },
        success: res => {
          if (res.data.code === 0) {
            this.setData({
                dynamic: res.data.data.needs,
                comments:res.data.data.comments
            })
          }
        }
    })
    return
    // 模拟数据
    const mockDynamic = {
      id: id,
      nickname: '温柔如风',
      age: 58,
      city: '北京市朝阳区',
      avatar: '/images/avatar/11.jpg',
      datingType: 'partner',
      content: '今天去公园散步，看到很多老人在跳舞，感觉生活很美好。希望能找到一个志同道合的伴侣一起享受晚年生活。',
      images: ['/images/avatar/11.jpg','/images/avatar/11.jpg','/images/avatar/11.jpg','/images/avatar/11.jpg'],
      createTime: '2小时前',
      likeCount: 12,
      commentCount: 3,
      isLiked: false
    };

    this.setData({
      dynamic: mockDynamic
    });
  },

  // 加载评论
  loadComments(id) {
    // 模拟评论数据
    const mockComments = [
      {
        id: 1,
        nickname: '阳光大叔',
        avatar: '/images/avatar/11.jpg',
        content: '生活很美好，加油！',
        time: '1小时前'
      },
      {
        id: 2,
        nickname: '优雅女士',
        avatar: '/images/avatar/11.jpg',
        content: '我也喜欢散步，有机会一起',
        time: '30分钟前'
      }
    ];

    this.setData({
      comments: mockComments
    });
  },

  // 预览图片
  previewImage(e) {
    const urls = e.currentTarget.dataset.urls;
    const current = e.currentTarget.dataset.current;
    wx.previewImage({
      urls: urls,
      current: current
    });
  },

  // 点赞/取消点赞
  toggleLike() {
    const dynamic = this.data.dynamic;
    dynamic.isLiked = !dynamic.isLiked;
    dynamic.likeCount = dynamic.isLiked ? dynamic.likeCount + 1 : dynamic.likeCount - 1;
    
    this.setData({
      dynamic: dynamic
    });
  },

  // 显示评论
  showComments() {
    this.setData({
      showComments: !this.data.showComments
    });
  }
}); 